/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_api_testing.Java_Util_GregorianCalendar.java]
 * Creation Date 	[15-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_api_testing;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * java.util.GregorianCalendar
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Java_Util_GregorianCalendar {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		Locale.setDefault(new Locale("en", "US"));
		// 构造当前日期
		GregorianCalendar calendar = new GregorianCalendar();

		int today = calendar.get(Calendar.DAY_OF_MONTH);
		int month = calendar.get(Calendar.MONTH);

		// 将日期设置为这个月的第一天, 并得到这一天是星期几
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

		// 获得一个星期的第一天(美国以Sunday为一周的第一天)
		int firstDayOfWeek = calendar.getFirstDayOfWeek();

		// 获得第一行的缩进
		int indent = 0;
		while (dayOfWeek != firstDayOfWeek) {
			indent++;
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		}

		// 获得一星期每天的英文简写
		String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
		do {
			System.out.printf("%4s", weekdayNames[dayOfWeek]);
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		} while (dayOfWeek != firstDayOfWeek);
		System.out.println();

		for (int i = 0; i <= indent; i++) {
			System.out.print(" ");
		}

		calendar.set(Calendar.DAY_OF_MONTH, 1);
		do {
			// 打印日期
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			System.out.printf("%3s", day);

			// 如果是当前日期则标记为 *
			if (day == today) {
				System.out.print('*');
			} else {
				System.out.print(' ');
			}

			// 向前移动日期
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			if (dayOfWeek == firstDayOfWeek) {
				System.out.println();
			}
		} while (calendar.get(Calendar.MONTH) == month);

		if (dayOfWeek != firstDayOfWeek) {
			System.out.println();
		}
	}
}
